package com.gohi.main.system.action;


import java.io.IOException;
import java.text.ParseException;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.gohi.core.web.action.BaseAction;
import com.gohi.main.store.hbm.ShopgooAppointmentCan;
import com.gohi.main.store.service.ICommentService;
import com.gohi.main.store.service.IStoreService;
import com.gohi.main.system.hbm.ShopgooArea;

import com.gohi.main.system.service.ISystemService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.user.service.IShopgooWxMemberService;

import com.gohi.main.util.StringUtil;

import com.gohi.main.util.WebUtils;
import com.gohi.main.util.Cache.MainCache;
/**
 * WEBAJAX 请求的action
 * 
 * @author Administrator
 * 
 */
public class ToolsAction extends BaseAction {
	Logger logger = Logger.getLogger(ToolsAction.class);
	private IShopgooWxMemberService shopgooWxMemberService;
	//商铺
	private IStoreService storeService;
	//订单和留言 技师时间 都在里面
	private  ICommentService  commentService;
	private ISystemService systemService;
	
	//清除缓存
	public void clear() throws IOException{
     	logger.info("正在初始化静态缓存信息.....");
		List<ShopgooArea> list =  systemService.getAre();
		MainCache.setArealist(list); //初始化区域的list
		MainCache.setBrandlist(storeService.queryAllBrand());//初始化品牌信息
		MainCache.setClasslist(storeService.getStoreClass());//获取服务分类信息
		MainCache.getBusinessmap().clear();
		MainCache.getAreaMap().clear();
		for(ShopgooArea are : list){
			//查询商圈信息
			MainCache.getBusinessmap().put(are.getArea_id(), systemService.getBusinessByareId(are.getArea_id().toString()));
			//查询级联的区域
			MainCache.getAreaMap().put(are.getArea_id(), systemService.getAreByparid(are.getArea_id().toString()));
		}
		logger.info("缓存初始化完毕..........");
		this.getResponse().getWriter().print("1");
	}
	
	//更新坐标
	public void setpoint(){
		String longitude  = this.getRequest().getParameter("longitude");
		String latitude  = this.getRequest().getParameter("latitude");
		WebUtils.addCookie(getResponse(), "point", longitude+"-"+latitude, -1);
	   //WebUtils.addCookie(getResponse(), "latitude", , -1);
		//暂时存cook里面去
		//System.err.println(latitude);
		
	}
	//添加收藏
	public void addFav() throws IOException{
		String type  = this.getRequest().getParameter("type");
		String id  = this.getRequest().getParameter("id");
		if(type!=null && id !=null){
			ShopgooMember user=  (ShopgooMember) this.getRequest().getSession().getAttribute("user");
			if(user==null){
				getResponse().getWriter().print("2");//未登陆
				return ;
			}
			int rs = storeService.addFav(type, id, user.getMember_id()+"");
			if(rs==0){
				getResponse().getWriter().print("3");//收藏过了
			}else{
				getResponse().getWriter().print("0");//收藏成功
			}
			
		}else{
			getResponse().getWriter().print("1");//参数错误
		}
		//WebUtils.addCookie(getResponse(), "point", longitude+"-"+latitude, -1);
	   //WebUtils.addCookie(getResponse(), "latitude", , -1);
		//暂时存cook里面去
		//System.err.println(latitude);
		
	}
	
	public void getTime() throws ParseException, IOException{
		String  hour = this.getRequest().getParameter("hour");
		String beautician_id = this.getRequest().getParameter("beautician_id");
		//System.err.println(hour+"技术ID："+beautician_id);
		//hour="60";
		//beautician_id="35";
		List<ShopgooAppointmentCan> list = commentService.getBeauticianTime(hour, beautician_id);
		Map<String, String> map =new LinkedHashMap<String, String>();
		String d = StringUtil.getDate("yyyy-MM-dd");
		for(int i=0;i<=3;i++){
			String key  =StringUtil.getNextDate(d, i);
			if(!map.containsKey(key)){
				map.put(key, "");
			}
		}
		int falg= 1;
		for(ShopgooAppointmentCan sh : list){
			String temp="";
			if(map.containsKey(sh.getAppt_date())==true){
			   //
				temp=temp+map.get(sh.getAppt_date());
			}
			//去计算
			//System.err.println(sh.getAppt_date());
			//如果技师是空的 并且 预约时间等于今天
			if(d.equals(sh.getAppt_date()) && beautician_id==null){
				falg=0;
			}else{
				falg=1;
			}
			temp = temp+StringUtil.splitTime(StringUtil.parentSimple(sh.getStart_hour(), "HH:mm"),StringUtil.parentSimple(sh.getEnd_hour(), "HH:mm"),Integer.parseInt(hour),falg);
			//入库
			map.put(sh.getAppt_date(), temp);
		}
		
	
		
   /* Iterator ite = map.keySet().iterator();
		while(ite.hasNext()){
			String v = (String) ite.next();
			System.err.println(v);
		}*/
		JSONObject js = JSONObject.fromObject(map);
		getResponse().setContentType("application/json;charset=GBK");
		//System.err.println(js.toString());
		getResponse().getWriter().print(js.toString());
	//	System.err.println(map.get(sh.getAppt_date()));
		
	}
	//默认的action
	public void index(){
		
	}
	public IShopgooWxMemberService getShopgooWxMemberService() {
		return shopgooWxMemberService;
	}
	public void setShopgooWxMemberService(
			IShopgooWxMemberService shopgooWxMemberService) {
		this.shopgooWxMemberService = shopgooWxMemberService;
	}
	public IStoreService getStoreService() {
		return storeService;
	}
	public void setStoreService(IStoreService storeService) {
		this.storeService = storeService;
	}
	public ICommentService getCommentService() {
		return commentService;
	}
	public void setCommentService(ICommentService commentService) {
		this.commentService = commentService;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}
	
	
}
